' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' Copyright (c) 2006 Microsoft Corporation. All rights reserved.

' Original code from the Microsoft Windows SDK for Windows Vista.

Imports System
Imports System.Xml
Imports System.IO
Imports System.Text.RegularExpressions
Imports Microsoft.Feeds.Interop

''' <summary>
''' Representation of an Item element in an RSS 2.0 XML document.
''' Zero or more RssItems are contained in an RssChannel.
''' </summary>
Public Class RssItem
    Implements IItem

    Private ReadOnly _title As String
    Private ReadOnly _description As String
    Private ReadOnly _link As String
    Private ReadOnly _enclosure As FileInfo

    Public ReadOnly Property Title() As String Implements IItem.Title
        Get
            Return Me._title
        End Get
    End Property
    Public ReadOnly Property Description() As String Implements IItem.Description
        Get
            Return Me._description
        End Get
    End Property
    Public ReadOnly Property Link() As String
        Get
            Return Me._link
        End Get
    End Property
    Public ReadOnly Property Enclosure() As FileInfo
        Get
            Return Me._enclosure
        End Get
    End Property

    ''' <summary>
    ''' Build an RSSItem from an XmlNode representing an Item element inside an RSS 2.0 XML document.
    ''' </summary>
    ''' <param name="itemNode"></param>
    Friend Sub New(ByVal itemNode As XmlNode)
        Dim selected As XmlNode
        selected = itemNode.SelectSingleNode("title")
        If Not selected Is Nothing Then
            Me._title = selected.InnerText
        End If

        selected = itemNode.SelectSingleNode("description")
        If Not selected Is Nothing Then
            Me._description = selected.InnerText
        End If

        selected = itemNode.SelectSingleNode("link")
        If Not selected Is Nothing Then
            Me._link = selected.InnerText
        End If

        selected = itemNode.SelectSingleNode("enclosure/@url")
        If Not selected Is Nothing Then
            Dim filename As String = selected.InnerText
            If Not filename Is Nothing Then
                Me._enclosure = New FileInfo("@" & filename)
            End If
        End If
    End Sub

    Friend Sub New(ByVal item As IFeedItem)
        Me._title = StripHTMLTags(item.Title)
        Me._link = item.Link

        Try
            Me._description = StripHTMLTags(item.Description)
        Catch e1 As ArgumentException
            Me._description = "<<<Item text too long.>>>"
        End Try

        Dim enclosure As IFeedEnclosure = CType(item.Enclosure, IFeedEnclosure)
        If Not enclosure Is Nothing Then
            Dim filename As String = GetLocalPath(enclosure)

            Try
                ' Let's make sure we only add pictures.
                Dim extension As String = "*" & Path.GetExtension(filename)
                extension = extension.ToUpperInvariant()
                If FeedList.ImageExtensions.Contains(extension) Then
                    ' Set enclosure without checking for existence, since enclosure might
                    ' not be on disk yet (BITS still getting it).
                    Me._enclosure = New FileInfo(filename)
                End If
            Catch e2 As ArgumentException
                System.Diagnostics.Debug.Print("Illegal character in filename.")
                ' 'Illegal character in filename' is one possible exception.
                ' Ignore exception; since we'll skip rssItems with this.enclosure == null
                ' higher up in the call chain.
            Catch e3 As NotSupportedException
                System.Diagnostics.Debug.Print("The given path's format is not supported.")
                ' 'Message="The given path's format is not supported."' is another possible exception.
                ' Ignore exception; since we'll skip rssItems with this.enclosure == null
                ' higher up in the call chain.
            End Try
        End If
    End Sub

    Private Shared Function GetLocalPath(ByVal encl As IFeedEnclosure) As String
        Try
            Return encl.LocalPath
        Catch ex As System.Runtime.InteropServices.COMException
            If ex.ErrorCode = -2147024735 Then
                System.Diagnostics.Debug.Print("Enclosure localPath is invalid.")
                ' "The specified path is invalid. (Exception from HRESULT: 0x800700A1)"
                ' ignore exception
            Else
                Throw
            End If
        Catch e1 As FileNotFoundException
            System.Diagnostics.Debug.Print("Enclosure localPath element not found exception.")
            ' ignore
        End Try
        Return Nothing
    End Function

    Private Shared Function StripHTMLTags(ByVal input As String) As String
        If Not Nothing Is input Then
            Dim rgx As Regex = New Regex("<[^>]*>", RegexOptions.Multiline)
            Return rgx.Replace(input, "")
        End If

        Return Nothing
    End Function
End Class